<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="en" />
    <title>skalibs: the alloc library interface</title>
    <meta name="Description" content="skalibs: the alloc library interface" />
    <meta name="Keywords" content="skalibs c unix alloc library libstddjb" />
    <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
  </head>
<body>

<p>
<a href="index.html">libstddjb</a><br />
<a href="../libskarnet.html">libskarnet</a><br />
<a href="../index.html">skalibs</a><br />
<a href="//skarnet.org/software/">Software</a><br />
<a href="//skarnet.org/">skarnet.org</a>
</p>

<h1> The <tt>alloc</tt> library interface </h1>

<p>
 The following functions are declared in the <tt>skalibs/alloc.h</tt> header,
and implemented in the <tt>libskarnet.a</tt> or <tt>libskarnet.so</tt> library.
</p>

<h2> General information </h2>

<p>
 <tt>alloc</tt> is the skalibs heap memory manager. It's actually a
wrapper for the
<a href="http://www.opengroup.org/onlinepubs/9699919799/functions/malloc.html">malloc()</a>
series of functions; it unifies a few system-dependent <tt>malloc</tt>
behaviours. It's also the API to implement and preload if for some
reason you need to plug in your own allocator: replacing <tt>alloc()</tt>
is much easier than replacing <tt>malloc()</tt> safely.
</p>

<p>
<strong> As a general rule, you should not be using the <tt>alloc</tt>
interface directly. </strong> Allocating and freeing individual cells
in the heap is a recipe for heap fragmentation, as well as cell
tracking nightmares leading to memory leaks. <strong> You should use
the higher-level <a href="stralloc.html">stralloc</a> and
<a href="genalloc.html">genalloc</a> interfaces </strong> to handle dynamic
arrays of objects.
</p>

<p>
 C's lack of automatic management of heap memory is not a drawback: it's
a feature of the language. It allows for code that is one or two orders
of magnitude faster than the equivalent in a higher-level language,
and very low on resources consumption. However, it requires more attention
from the programmer. Good APIs can significantly reduce the difficulty of
keeping track of every heap-allocated cell, and every smart programmer
should favor them over basic interfaces like <tt>malloc()</tt>.
</p>

<p>
 <tt>alloc</tt> is used internally by skalibs to implement
<a href="stralloc.html">stralloc</a>, and nowhere else.
</p>

<h2> Functions </h2>

<p>
<code> char *alloc (size_t len) </code> <br />
Allocates a block of <em>len</em> bytes in the heap and returns a pointer
to the start of the block (or NULL if it failed). Though the pointer type
is <tt>char *</tt>, the block of memory is correctly aligned for any type
of object. If <em>len</em> is 0, the function returns a pointer that
cannot be written to, but that is <em>not null</em>. Note that this is
different from the required C99 behaviour for <tt>malloc()</tt>.
</p>

<p>
<code> void alloc_free (void *p) </code> <br />
Frees the block of heap memory pointed to by <em>p</em>.
</p>

<p>
<code> int alloc_realloc (char **p, size_t newlen) </code> <br />
Redimension the block of heap memory pointed to by *<em>p</em> to
<em>newlen</em> bytes. The block may have to be moved, in which case
*<em>p</em> will be modified. Normally returns 1; if an error occurred,
returns 0 and sets errno, and neither *<em>p</em> nor its contents are
modified.
</p>

<p>
<code> int alloc_re (char **p, size_t oldlen, size_t newlen) </code> <br />
Legacy interface for reallocation. It works like <tt>alloc_realloc</tt>,
except that the original block length must be provided as the <em>oldlen</em>
argument.
</p>

</body>
</html>
